// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SocialAuthBrickRedController.java

package com.nervytech.tutorcruise.spring.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.brickred.socialauth.AuthProvider;
import org.brickred.socialauth.SocialAuthManager;
import org.brickred.socialauth.spring.bean.SocialAuthTemplate;
import org.brickred.socialauth.util.SocialAuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/reg/social")
public class SocialAuthBrickRedController
{
	
    private String baseCallbackUrl;
    private String successPageURL;
    private String accessDeniedPageURL;
    
    @Autowired
    private SocialAuthTemplate socialAuthTemplate;
    
    @Autowired
    private SocialAuthManager socialAuthManager;
    
    private final Log LOG = LogFactory.getLog(getClass());

    public SocialAuthBrickRedController()
    {
        baseCallbackUrl = "http://localhost:18080/tutorcruise/pub/socialauth";
        successPageURL = "/pub/social?action=success";
        accessDeniedPageURL = "/pub/social?action=failed";
    }

    private String connect(String providerId, HttpServletRequest request)
        throws Exception
    {
        LOG.debug((new StringBuilder("Getting Authentication URL for :")).append(providerId).toString());
        String callbackURL = (new StringBuilder(String.valueOf(request.getScheme()))).append("://").append(request.getServerName()).append(":").append(request.getServerPort()).append(request.getRequestURI()).toString();
        String url = socialAuthManager.getAuthenticationUrl(providerId, callbackURL);
        if(callbackURL.equals(url))
        {
            url = getSuccessPathURL(request);
            socialAuthManager.connect(new HashMap());
        }
        socialAuthTemplate.setSocialAuthManager(socialAuthManager);
        return (new StringBuilder("redirect:")).append(url).toString();
    }

    private String oauthCallback(HttpServletRequest request)
    {
        callback(request);
        return (new StringBuilder("redirect:")).append(getSuccessPathURL(request)).toString();
    }

    private String oauth2Callback(HttpServletRequest request)
    {
        callback(request);
        return (new StringBuilder("redirect:")).append(getSuccessPathURL(request)).toString();
    }

    private String hotmailCallback(HttpServletRequest request)
    {
        callback(request);
        return (new StringBuilder("redirect:")).append(getSuccessPathURL(request)).toString();
    }

    private String openidCallback(HttpServletRequest request)
    {
        callback(request);
        return (new StringBuilder("redirect:")).append(getSuccessPathURL(request)).toString();
    }

    private void callback(HttpServletRequest request)
    {
        SocialAuthManager m = socialAuthTemplate.getSocialAuthManager();
        if(m != null)
            try
            {
                AuthProvider provider = m.connect(SocialAuthUtil.getRequestParametersMap(request));
                System.out.println((new StringBuilder("PROVIDDDDDDDDDDDDDDDDDDDDEEEEEEEEE ")).append(provider.toString()).toString());
                LOG.debug((new StringBuilder("Connected Provider : ")).append(provider.getProviderId()).toString());
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        else
            LOG.debug("Unable to connect provider because SocialAuthManager object is null.");
    }

    private String fbCancel(String error, HttpServletRequest request)
    {
        LOG.debug((new StringBuilder("Facebook send an error : ")).append(error).toString());
        if("user_denied".equals(error))
            return (new StringBuilder("redirect:")).append(getFailureURL(request)).toString();
        else
            return "redirect:/";
    }

    private String googleCancel(String error, HttpServletRequest request)
    {
        LOG.debug((new StringBuilder("Google send an error : ")).append(error).toString());
        if("cancel".equals(error))
            return (new StringBuilder("redirect:")).append(getFailureURL(request)).toString();
        else
            return "redirect:/";
    }

    private String hotmailCancel(String error, HttpServletRequest request)
    {
        LOG.debug((new StringBuilder("Hotmail send an error : ")).append(error).toString());
        if("user_denied".equals(error))
            return (new StringBuilder("redirect:")).append(getFailureURL(request)).toString();
        else
            return "redirect:/";
    }

    private String myspaceCancel(String error, HttpServletRequest request)
    {
        LOG.debug((new StringBuilder("MySpace send an error : ")).append(error).toString());
        if("user_refused".equals(error))
            return (new StringBuilder("redirect:")).append(getFailureURL(request)).toString();
        else
            return "redirect:/";
    }

    public String getSuccessPathURL(HttpServletRequest request)
    {
        System.out.println((new StringBuilder("URIIIIIIIII ")).append(request.getRequestURL()).append(" SERRRRR ").append(request.getServletPath()).toString());
        String successURL = "/pub/social?action=authsuccess";
        if(request.getServletPath().equalsIgnoreCase("/reg"))
            successURL = "/pub/social?action=regsuccess";
        return successURL;
    }

    public String getFailureURL(HttpServletRequest request)
    {
        String failureURL = "/pub/social?action=authfailed";
        if(request.getServletPath().equalsIgnoreCase("/reg"))
            failureURL = "/pub/social?action=regfailed";
        return failureURL;
    }

}
